<?php
class ModelSaleMailTemplate extends Model {

	public function addMailTemplate($data) {
		return $this->db->insert('mail_template', $data);
	}

	public function editMailTemplate($mail_template_id, $data) {
		return $this->db->update('mail_template', $data, "mail_template_id = $mail_template_id");
	}

	public function getMailTemplate($mail_template_id) {
		return $this->db->get('mail_template', "mail_template_id = $mail_template_id");
	}

	public function getMailTemplateByCode($code) {
		return $this->db->get('mail_template', "code = '$code'");
	}
	
	public function deleteMailTemplate($mail_template_id) {
		if (is_array($mail_template_id)) $mail_template_id = implode(',', $mail_template_id);
		$this->db->runSql("DELETE FROM @@mail_template WHERE mail_template_id IN ($mail_template_id)");
	}

	public function getMailTemplates($filter = null) {
		$sql = "SELECT mail_template_id,code,title FROM @@mail_template WHERE 1";
		if ($filter) {
			$sql .= $filter->sort('code');
			$sql .= $filter->limit();
		}
		$query = $this->db->query($sql);
		return $query->rows;
	}

	public function getTotalMailTemplates() {
		$sql = "SELECT COUNT(*) AS total FROM @@mail_template WHERE 1";		
		return $this->db->queryOne($sql);
	}
}